package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@Slf4j
@Api(tags = "菜品相关接口")
@RestController("adminDishController")
@RequestMapping("/admin/dish")
public class DishController {

    private static final String DISH_CATEGORY_CACHE = "dish_category_";

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result saveWithFlavor(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);

        // 清除菜品的分类查询Redis缓存
        cleanCache(dishDTO.getCategoryId().toString());

        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQuery
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQuery){
        log.info("菜品分页查询：{}", dishPageQuery);
        PageResult pageResult = dishService.pageQuery(dishPageQuery);
        return Result.success(pageResult);
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("删除菜品：{}", ids);
        dishService.deleteByIds(ids);
        // 清除所有的菜品相关Redis缓存
        cleanCache();
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result getById(@PathVariable Long id){
        log.info("根据id查询菜品：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        if (dishVO == null){
            return Result.error(MessageConstant.DISH_NOT_FOUND);
        }
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        // 清除所有的菜品相关Redis缓存
        cleanCache();
        return Result.success();
    }

    /**
     * 启用停售菜品
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用停售菜品")
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("停售起售菜品：{},{}", status, id);
        dishService.startOrStop(status, id);
        // 清除菜品的分类查询Redis缓存
        cleanCache(dishService.getByIdWithFlavor(id).getCategoryId().toString());
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result list(Long categoryId){
        log.info("查询菜品分类：{}", categoryId);
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    /**
     * 清除缓存 -- 指定菜品分类缓存
     * @param patten
     */
    private void cleanCache(String patten) {
        patten = DISH_CATEGORY_CACHE + patten;
        log.info("清除缓存：{}", patten);
        Set keys = redisTemplate.keys(patten);
        redisTemplate.delete(keys);
    }

    /**
     * 清除缓存 -- 全部菜品分类缓存
     */
    private void cleanCache() {
        cleanCache("");
    }
}
